package cc.wanforme.fsync.web;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class FrontController {

    @Value("${front-path}")
    private String frontPath;

    @GetMapping("/")
    public void website(HttpServletResponse response) throws Exception {
        response.sendRedirect(frontPath);
    }

    /** 此请求用于映射前端的静态资源，前端url最终会404，然后交给 ErrorController 处理，并重定向到前端首页 */
    @GetMapping("${front-path}/**")
    public void appRoot(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String loc = request.getRequestURI().substring(frontPath.length());
        request.getRequestDispatcher("/dist" + loc).forward(request, response);
    }

}
